import { Component, OnInit } from '@angular/core';

import { Pillar } from '../model/pillar-model';
import { BirdFlyService } from '../bird-fly/service/bird-fly.service';
import { Observable } from 'rxjs/Rx';
import { subscribeOn } from '_rxjs@5.5.2@rxjs/operator/subscribeOn';
import { Subscription } from '_rxjs@5.5.2@rxjs/Subscription';
import { fadeInFromTop } from '../../../animations/fade-in-from-top';
import { fadeInFromBottom } from '../../../animations/fade-in-from-bottom';

@Component({
    selector: 'pillar',
    templateUrl: "./pillar.component.html",
    styleUrls: ['./pillar.component.css'],
    animations: [fadeInFromTop, fadeInFromBottom]
})

export class PillarComponent implements OnInit {
    pillars: Pillar[];
    obIndex: number;

    constructor(public birdFlyService: BirdFlyService) { }
    ngOnInit() {
        //每次移动距离
        let observer = data => {
            if(this.birdFlyService.getBird().status == "over"){
                this.birdFlyService.cancleOneSubscription(this.obIndex);
            }
            let length: number = this.birdFlyService.getLength();
            let pillars: Pillar[] = this.birdFlyService.getPillars();
            for (let pillar of pillars) {
                pillar.x = pillar.x - length;
                pillar.state = this.birdFlyService.ifReach(pillar);
                if(pillar.state){
                    this.birdFlyService.calcKnocked(pillar);
                }
            }
            if (data % (18 / length) == 0) {
                let center = 30 + 50 * Math.random();

                pillars.push(
                    { x: 110, y1: center - 13 - 20 * Math.random(), y2: center + 13, state: false }
                );
                console.log(pillars)
            }
            if (pillars.length > 9) {
                pillars.splice(0, 1);
            }
            this.pillars = pillars;
        }
        this.obIndex = this.birdFlyService.addObserver(observer);
    }
}